package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.domain.OrderStatistics;
import org.locker.domain.bo.OrderStatisticsBo;
import org.locker.domain.vo.OrderStatisticsVo;
import org.locker.mapper.OrderStatisticsMapper;
import org.locker.service.IOrderStatisticsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单统计Service业务层处理
 *
 * @author winnie
 * @date 2024-04-29
 */
@RequiredArgsConstructor
@Service
public class OrderStatisticsServiceImpl  extends ServiceImpl<OrderStatisticsMapper, OrderStatistics>  implements IOrderStatisticsService {

    private final OrderStatisticsMapper baseMapper;

    /**
     * 查询订单统计
     */
    @Override
    public OrderStatisticsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单统计列表
     */
    @Override
    public TableDataInfo<OrderStatisticsVo> queryPageList(OrderStatisticsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderStatistics> lqw = buildQueryWrapper(bo);
        Page<OrderStatisticsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单统计列表
     */
    @Override
    public List<OrderStatisticsVo> queryList(OrderStatisticsBo bo) {
        LambdaQueryWrapper<OrderStatistics> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderStatistics> buildQueryWrapper(OrderStatisticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderStatistics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPositionId() != null, OrderStatistics::getPositionId, bo.getPositionId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), OrderStatistics::getName, bo.getName());
        lqw.eq(bo.getDateTag() != null, OrderStatistics::getDateTag, bo.getDateTag());
        lqw.eq(bo.getCount() != null, OrderStatistics::getCount, bo.getCount());
        lqw.eq(bo.getIncome() != null, OrderStatistics::getIncome, bo.getIncome());
        lqw.eq(bo.getStatisticsPrice() != null, OrderStatistics::getStatisticsPrice, bo.getStatisticsPrice());
        return lqw;
    }

    /**
     * 新增订单统计
     */
    @Override
    public Boolean insertByBo(OrderStatisticsBo bo) {
        OrderStatistics add = MapstructUtils.convert(bo, OrderStatistics.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单统计
     */
    @Override
    public Boolean updateByBo(OrderStatisticsBo bo) {
        OrderStatistics update = MapstructUtils.convert(bo, OrderStatistics.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OrderStatistics entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
